import { Loading, Message } from 'element-ui'
import Vue from 'vue'
import axios from 'axios'
import router from '../../router'
// 配置请求的根路径
axios.defaults.baseURL = "/proxy_url"
// 挂载到原型，全局使用
Vue.prototype.$http = axios


// 加载动画
let loading =  null
// 需要执行加载动画的事件数
let loadingNum = 0
// 开始加载动画
function startLoading() {
  if (loadingNum == 0){
    loading = Loading.service({
      lock: true,
      text: '拼命加载中...',
      background:'rgba(255,255,255,0.5)',
    })
  }
  loadingNum++;
}
// 结束加载动画
function endLoading() {
  loadingNum--;
  if (loadingNum <= 0) {
    loading.close()
  }
}

// 为每次一请求都在header中携带token
axios.interceptors.request.use(config => {
  startLoading();
  // console.log(window.sessionStorage.getItem("token"))
  // 每一次请求都携带
  config.headers.Authorization = window.sessionStorage.getItem("token");
  return config;
}, err => {
  return Promise.reject(err);
})

// 
axios.interceptors.response.use(config => {
  endLoading();
  return config;
}, err => {
  // console.log(err);
  endLoading();
  if (err && err.response) {
    switch (err.response.status) {
      case 400: err.message = '请求错误(400)'; break;
      case 401: router.push('/login'); break;
      case 403: err.message = '拒绝访问(403)'; break;
      case 404: err.message = '请求出错(404)'; break;
      case 408: err.message = '请求超时(408)'; break;
      case 500: err.message = '服务器错误(500)'; break;
      case 501: err.message = '服务未实现(501)'; break;
      case 502: err.message = '网络错误(502)'; break;
      case 503: err.message = '服务不可用(503)'; break;
      case 504: err.message = '网络超时(504)'; break;
      case 505: err.message = 'HTTP版本不受支持(505)'; break;
      default: err.message = `连接出错(${err.response.status})!`;
    }
  } else {
    err.message = '连接服务器失败!'
  }
  Message.error(err.message)
  return {
    data: {
      success: false,
      data: null,
      errorMsg: err.message
    }
  };
})

export default {
  // 用户id
  userid: null,
  // 用户名
  username: null,
  // 存储菜单
  menuList: [
  ],// 加载动画
  loading: loading,
  // 需要执行加载动画的事件数
  loadingNum: loadingNum,
  // 开始加载动画函数
  startLoading: startLoading,
  // 结束加载动画函数
  endLoading: endLoading,
  // 数据重构
  getChild(list, pStatus=1) {
    let outList = [];
    let childrenNum = 0;
    for (let index = 0; index < list.length; index++) {
      let children = {outList: [], childrenNum: 0};
      if (list[index].children.length != 0) {
        children = this.getChild(list[index].children, list[index].data.status)
      }
      if (list[index].data.status == 1) {
        childrenNum++;
      }
      outList.push({
        // 菜单id
        menuId: list[index].data.id,
        // 菜单图标
        icon: list[index].data.icon,
        // 菜单名称
        label: list[index].data.title,
        // 菜单地址
        menuURL: list[index].data.url,
        // 菜单类型
        menuType: list[index].data.type,
        // type: list[index].data.type == 1 ? '一级菜单' : list[index].data.type == 2 ? '二级菜单' : '不是菜单',
        // 菜单状态
        menuStatus: list[index].data.status,
        // status: list[index].data.status == 1 ? '正常' : list[index].data.status == 2 ? '冻结' : '删除',
        // 菜单备注
        menuRemark: list[index].data.remark,
        // 菜单权限标识
        menuPerms: list[index].data.perms,
        // 子菜单
        children: children.outList,
        childrenNum: children.childrenNum,
        // 父菜单状态
        pStatus: pStatus,
        // 父菜单id
        pId: list[index].data.pid,
        // 菜单创建时间
        createDate: list[index].data.createDate,
        // 菜单更新时间
        updateDate: list[index].data.updateDate
      });
    }
    return {outList: outList, childrenNum: childrenNum};
  },
  // 获取树状结构菜单
  async getMenu() {
    const {data: result} = await Vue.prototype.$http.get('/menu/tree');
    if (result.success) {
      this.menuList = this.getChild(result.data).outList;
      // console.log(this.menuList)
      // console.log("已经执行获取树状结构菜单")
    }
  }
}